Skip to content

Conversation

@minggangw
Copy link
Member

@minggangw minggangw commented Aug 9, 2025

This PR adds functions to run ROS2 package executables and launch files programmatically from within Node.js applications using the ros2 run and ros2 launch commands.

  • Adds ros2Run() function to execute ROS2 package executables with optional arguments
  • Adds ros2Launch() function to run ROS2 launch files with optional arguments
  • Includes comprehensive input validation for both functions

Fix: #1220

Copilot AI review requested due to automatic review settings August 9, 2025 09:33
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functions to run ROS2 package executables and launch files programmatically from within Node.js applications using the ros2 run and ros2 launch commands.

  • Adds ros2Run() function to execute ROS2 package executables with optional arguments
  • Adds ros2Launch() function to run ROS2 launch files with optional arguments
  • Includes comprehensive input validation for both functions

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 6 comments.

File Description
index.js Implements ros2Run and ros2Launch functions with input validation and child process spawning
test/types/index.test-d.ts Adds type definitions for the new functions to ensure proper TypeScript support


const command = 'ros2';
const cmdArgs = ['run', packageName, executableName, ...args];
const childProcess = spawn(command, cmdArgs);
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spawn call lacks proper validation of the command and arguments, which could lead to command injection if packageName, executableName, or args contain shell metacharacters. Consider sanitizing inputs or using shell: false option explicitly.

Suggested change
const childProcess = spawn(command, cmdArgs);
const childProcess = spawn(command, cmdArgs, { shell: false });

Copilot uses AI. Check for mistakes.
}
const command = 'ros2';
const cmdArgs = ['launch', packageName, launchFile, ...args];
const childProcess = spawn(command, cmdArgs);
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spawn call lacks proper validation of the command and arguments, which could lead to command injection if packageName, launchFile, or args contain shell metacharacters. Consider sanitizing inputs or using shell: false option explicitly.

Suggested change
const childProcess = spawn(command, cmdArgs);
const childProcess = spawn(command, cmdArgs, { shell: false });

Copilot uses AI. Check for mistakes.
index.js Outdated
});

resolve({
process: childProcess,
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function resolves immediately after spawning the process without waiting for it to start successfully. Consider waiting for the 'spawn' event or checking if the process is running before resolving to provide better error handling.

Suggested change
process: childProcess,
childProcess.once('error', (error) => {
reject(new Error(`Failed to start ros2 run: ${error.message}`));
});
childProcess.once('spawn', () => {
resolve({
process: childProcess,
});

Copilot uses AI. Check for mistakes.

resolve({
process: childProcess,
});
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function resolves immediately after spawning the process without waiting for it to start successfully. Consider waiting for the 'spawn' event or checking if the process is running before resolving to provide better error handling.

Suggested change
});
// Wait for immediate errors before resolving
childProcess.once('error', (error) => {
reject(new Error(`Failed to start ros2 launch: ${error.message}`));
});
setImmediate(() => {
// If no error occurred, resolve
resolve({
process: childProcess,
});
});

Copilot uses AI. Check for mistakes.
index.js Outdated
*/
ros2Launch(packageName, launchFile, args = []) {
return ros2Launch(packageName, launchFile, args);
},
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The method is a simple wrapper around the standalone function. Consider removing the duplication by either using the standalone function directly or implementing the logic only in the object method.

Suggested change
},

Copilot uses AI. Check for mistakes.
index.js Outdated
*/
ros2Launch(packageName, launchFile, args = []) {
return ros2Launch(packageName, launchFile, args);
},
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The method is a simple wrapper around the standalone function. Consider removing the duplication by either using the standalone function directly or implementing the logic only in the object method.

Suggested change
},
ros2Launch: ros2Launch,

Copilot uses AI. Check for mistakes.
@minggangw minggangw requested a review from Copilot August 9, 2025 09:47

This comment was marked as outdated.

@coveralls
Copy link

Coverage Status

coverage: 84.526%. remained the same
when pulling 73e2be0 on minggangw:fix-1220
into 95b5609 on RobotWebTools:develop.

@minggangw minggangw requested a review from Copilot August 9, 2025 10:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds functionality to run ROS2 package executables and launch files programmatically from Node.js applications using the ros2 run and ros2 launch commands.

  • Adds ros2Run() function to execute ROS2 package executables with optional arguments and input validation
  • Adds ros2Launch() function to run ROS2 launch files with optional arguments and input validation
  • Includes TypeScript type definitions for both new functions

Reviewed Changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
index.js Implements ros2Run and ros2Launch functions with validation and spawns child processes
test/types/index.test-d.ts Adds TypeScript type tests for the new ros2Run and ros2Launch functions

childProcess.on('error', (error) => {
reject(new Error(`Failed to start ros2 run: ${error.message}`));
});
childProcess.on('spawn', () => {
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'spawn' event is not standard in Node.js child_process. Use 'childProcess.pid' check or a timeout to confirm successful spawn instead of relying on a non-existent event.

Copilot uses AI. Check for mistakes.
resolve({
process: childProcess,
});
});
Copy link

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'spawn' event is not standard in Node.js child_process. Use 'childProcess.pid' check or a timeout to confirm successful spawn instead of relying on a non-existent event.

Suggested change
});
// The 'spawn' event is not standard; instead, check for childProcess.pid
if (childProcess.pid) {
resolve({
process: childProcess,
});
} else {
// Fallback: wait a short time to see if pid is set
setTimeout(() => {
if (childProcess.pid) {
resolve({
process: childProcess,
});
} else {
reject(new Error('Failed to spawn ros2 process'));
}
}, 50);
}

Copilot uses AI. Check for mistakes.
@minggangw minggangw merged commit a5f8aec into RobotWebTools:develop Aug 10, 2025
19 checks passed
minggangw added a commit that referenced this pull request Aug 18, 2025
This PR adds functions to run ROS2 package executables and launch files programmatically from within Node.js applications using the `ros2 run` and `ros2 launch` commands.

- Adds `ros2Run()` function to execute ROS2 package executables with optional arguments
- Adds `ros2Launch()` function to run ROS2 launch files with optional arguments
- Includes comprehensive input validation for both functions

Fix: #1220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for running ros2 run and ros2 launch commands from rclnodejs

2 participants